package com.hnxxxy.sysmgr.mapper;


import java.util.List;

import com.hnxxxy.common.redis.RedisMyabtisCstCache;
import com.hnxxxy.entity.RoleExample;
import com.hnxxxy.sysmgr.pojo.Role;
import com.hnxxxy.sysmgr.pojo.Role;
import com.hnxxxy.sysmgr.vo.MenuVo;
import com.hnxxxy.sysmgr.vo.RoleVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
@CacheNamespace(implementation = RedisMyabtisCstCache.class)
public interface RoleMapper {
    /**
     * 删除sys_staff_role中间表数据
     * @param id
     */
    @Delete({"delete from sys_staff_role where staffid = #{id}"})
    void deleteRoleStaffByStaffid(Integer id);

    /**
     * 查询角色信息
     * @param role
     * @return
     */
    @Select({"<script>",
            "select * from sys_role ",
            "<where> ",
            "<if test='rolename!=null'>",
            "and rolename like concat('%',#{rolename},'%')",
            "</if>",
            "<if test='remark!=null'>",
            "and remark like concat('%',#{remark},'%')",
            "</if>",
            "</where>",
            "</script>"})
    List<Role> queryAllRole(Role role);

    /**
     * 添加角色信息
     * @param roleVo
     */
    @Insert({"insert into sys_role (rolename, state, remark, createtime)" +
            " values (#{rolename},#{state},#{remark},#{createtime})"})
    void insertRole(RoleVo roleVo);

    /**
     * 删除角色信息
     * @param id
     */
    @Delete({"delete from sys_role where id = #{id}"})
    void deleteRole(Integer id);


    /**
     * 更新角色信息
     * @param roleVo
     * @return
     */
    @Update({"<script>",
            "update sys_role",
            "<set>",
            "<if test='rolename!=null'>",
            " rolename = #{rolename},",
            "</if>",
            "<if test='state!=null'>",
            "state=#{state},",
            "</if>",
            "<if test='remark!=null'>",
            " remark=#{remark},",
            "</if>",
            "<if test='createtime!=null'>",
            "createtime=#{createtime},",
            "</if>",
            "</set>",
            "where id=#{id}",
            "</script>"})
    int updateRole(RoleVo roleVo);


    /**
     * 加载角色管理的分配角色的数据
     * @param available
     * @param id
     * @return
     */
    @Select({"select t1.* from sys_role t1 inner join sys_staff_role t2 on(t1.id=t2.roleid) where t2.staffid=#{id} and t1.state=#{available}"})
    List<Role> queryRoleByStaffid(@Param("available") Integer available, @Param("id") Integer id);

    /**
     * 根据角色id删除sys_staff_role里面的数据
     * @param id
     */
    @Delete("delete from sys_staff_role where roleid=#{id}")
    void deleteRoleStaffByRid(Integer id);

    /**
     * 根据rid删除sys_role_token里面所有数据
     * @param rid
     */
    @Delete("delete from sys_role_token where roleid=#{rid}")
    void deleteRoleTokenByRid(Integer rid);

    /**
     * 保存角色和权限的关系
     * @param rid
     * @param tid
     */
    @Insert({"insert into sys_role_token(roleid,tokenid) values(#{rid},#{tid})"})
    void insertRoleToken(Integer rid, Integer tid);
}